//
//  BYMainViewController.m
//  School+
//
//  Created by Sean Sebastian on 12/3/15.
//  Copyright © 2015 Sean Sebastian. All rights reserved.
//

#import "BYMainViewController.h"

#import "BYHomeTableViewController.h"
#import "BYChatTableViewController.h"
#import "BYFunctionViewController.h"
#import "BYDiscoverViewController.h"
#import "BYProfileTableViewController.h"

#import "CalendarViewController.h"
#import "NotesViewController.h"

@interface BYMainViewController ()

@property (nonatomic , weak) UIButton *centerButton;


// center function
@property (nonatomic , weak) UIButton *toNavigationBtn;
@property (nonatomic , weak) UIButton *fromNavigationBtn;
@property (nonatomic , weak) UIButton *checkClassRoom;
@property (nonatomic , weak) UIButton *checkLibrary;
@property (nonatomic , weak) UIButton *checkKebiao;
@property (nonatomic , weak) UIButton *notesBtn;
@property (nonatomic , weak) UIButton *calendarBtn;
@property (nonatomic , weak) UIButton *LeaveFunctionBtn_No1;

@end

@implementation BYMainViewController

// 中间按钮点击事件
- (void)centerButtonClick
{
    NSLog(@"点击了中间的按钮！");
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.backView.alpha = 0.8f;
        
    } completion:^(BOOL finished) {
        self.backView.hidden = NO;
    }];
    
    
    
    
    
}

- (void)backViewClick
{
    [UIView animateWithDuration:0.5 animations:^{
        self.backView.alpha = 0;
        
    } completion:^(BOOL finished) {
        self.backView.hidden = YES;
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.backView];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backViewClick)];
    
    [self.backView addGestureRecognizer:tapGesture];
    
    
    BYHomeTableViewController *htvc = [[BYHomeTableViewController alloc] init];    // tableViewVC
    [self addChildViewController:htvc andTitle:@"主页" andImageName:@"tabbar_home"];
    
//    BYChatTableViewController *ctvc = [[BYChatTableViewController alloc] init];     // tableViewVC
//    [self addChildViewController:ctvc andTitle:@"聊天" andImageName:@"tabbar_home"];

    BYFunctionViewController *fvc = [[BYFunctionViewController alloc] init];        // ViewVC
    [self addChildViewController:fvc andTitle:@" " andImageName:@"s"];
    // CUICatalog: Invalid asset name supplied:
    // 原因是UIImage中的imageNamed方法传入了空值，解决，传入值，即使图片不存在
    

//    BYDiscoverViewController *dvc = [[BYDiscoverViewController alloc] init];        // tableViewVC
//    [self addChildViewController:dvc andTitle:@"发现" andImageName:@"tabbar_home"];
    

    BYProfileTableViewController *ptvc = [[BYProfileTableViewController alloc] init];       // tableViewVC
    [self addChildViewController:ptvc andTitle:@"我" andImageName:@"tabbar_profile"];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.centerButton addTarget:self action:@selector(centerButtonClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)addChildViewController:(UIViewController *)childController andTitle:(NSString *)title andImageName:(NSString *)imageName
{
    childController.title = title;
    
    childController.tabBarItem.image = [UIImage imageNamed:imageName];
    
    [self addChildViewController:[[UINavigationController alloc] initWithRootViewController:childController]];
    
}

- (UIButton *)centerButton
{
    if ( _centerButton == nil ) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        CGFloat width = self.tabBar.bounds.size.width / 3;
        
        button.frame = CGRectMake(width + 40 , 0, 50 , 50);
        
        _centerButton = button;
        
        [self.tabBar addSubview:button];
        
        
        NSLog(@"%@" , button);
    }
    
    
    return _centerButton;
}

- (UIView *)backView
{
    if (_backView == nil) {
        UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
        view.backgroundColor = [UIColor blackColor];
        view.alpha = 0.8f;
        view.hidden = YES;
        
        [view addSubview:self.toNavigationBtn];
        [view addSubview:self.fromNavigationBtn];
        [view addSubview:self.checkClassRoom];
        [view addSubview:self.checkLibrary];
        [view addSubview:self.checkKebiao];
        [view addSubview:self.notesBtn];
        [view addSubview:self.calendarBtn];
        [view addSubview:self.LeaveFunctionBtn_No1];
        
        
        _backView = view;
        return view;
    }
    return _backView;
}



///////////////////////////////////////////////////////////////////////////
/*
 @property (nonatomic , weak) UIButton *toNavigationBtn;
 @property (nonatomic , weak) UIButton *fromNavigationBtn;
 @property (nonatomic , weak) UIButton *checkClassRoom;
 @property (nonatomic , weak) UIButton *checkLibrary;
 @property (nonatomic , weak) UIButton *checkKebiao;
 @property (nonatomic , weak) UIButton *notesBtn;
 @property (nonatomic , weak) UIButton *calendarBtn;
 @property (nonatomic , weak) UIButton *LeaveFunctionBtn_No1;

 */

#define btnWidthAndHeight 100

#define baseY 50
#define btn1X 80
#define btn2X 180
#define margin 20
- (UIButton *)toNavigationBtn
{
    if (_toNavigationBtn == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:@"来校导航" forState:UIControlStateNormal];
        btn.frame = CGRectMake(btn1X, baseY, btnWidthAndHeight , btnWidthAndHeight);
        btn.backgroundColor = [UIColor whiteColor];
        _toNavigationBtn = btn;
        return btn;
    }
    return _toNavigationBtn;
}

- (UIButton *)fromNavigationBtn
{
    if (_fromNavigationBtn == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:@"出游导航" forState:UIControlStateNormal];
        btn.frame = CGRectMake(btn2X + margin, baseY, btnWidthAndHeight , btnWidthAndHeight);
        btn.backgroundColor = [UIColor whiteColor];
        _fromNavigationBtn = btn;
        return btn;
    }
    return _fromNavigationBtn;
}
- (UIButton *)checkClassRoom
{
    if (_checkClassRoom == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:@"查教室" forState:UIControlStateNormal];
        btn.frame = CGRectMake(btn1X, baseY + btnWidthAndHeight + margin , btnWidthAndHeight, btnWidthAndHeight);
        btn.backgroundColor = [UIColor whiteColor];
        _checkClassRoom = btn;
        return btn;
    }
    return _checkClassRoom;
}
- (UIButton *)checkLibrary
{
    if (_checkLibrary == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:@"图书馆检索" forState:UIControlStateNormal];
        btn.frame = CGRectMake(btn2X + margin, baseY + btnWidthAndHeight + margin , btnWidthAndHeight, btnWidthAndHeight);
        btn.backgroundColor = [UIColor whiteColor];
        _checkLibrary = btn;
        return btn;
    }
    return _checkLibrary;
}
- (UIButton *)checkKebiao
{
    if (_checkKebiao == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:@"查课表" forState:UIControlStateNormal];
        btn.frame = CGRectMake(btn1X , baseY + btnWidthAndHeight * 2 + margin *2, btnWidthAndHeight, btnWidthAndHeight);
        btn.backgroundColor = [UIColor whiteColor];
        _checkKebiao = btn;
        return btn;
    }
    return _checkKebiao;
}
- (UIButton *)notesBtn
{
    if (_notesBtn == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:@"备忘录" forState:UIControlStateNormal];
        btn.frame = CGRectMake(btn2X+ margin, baseY + btnWidthAndHeight *2 + margin*2, btnWidthAndHeight, btnWidthAndHeight);
        [btn addTarget:self action:@selector(notesBtnClick) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor whiteColor];
        _notesBtn = btn;
        return btn;
    }
    return _notesBtn;
}
//NotesViewController
- (void)notesBtnClick
{
    printf(__FUNCTION__);
    
    UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:[[NotesViewController alloc] init]];
    
    [self presentViewController:nvc animated:YES completion:nil];
}


- (UIButton *)calendarBtn
{
    if (_calendarBtn == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:@"日历" forState:UIControlStateNormal];
        btn.frame = CGRectMake(btn1X, baseY + btnWidthAndHeight *3 + margin *3,  btnWidthAndHeight, btnWidthAndHeight);
        [btn addTarget:self action:@selector(calendarBtnClick) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor whiteColor];
        _calendarBtn = btn;
        return btn;
    }
    return _calendarBtn;
}

- (void)calendarBtnClick
{
    printf(__FUNCTION__);

    UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:[[CalendarViewController alloc] init]];

    [self presentViewController:nvc animated:YES completion:nil];
}

- (UIButton *)LeaveFunctionBtn_No1
{
    if (_LeaveFunctionBtn_No1 == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:@"预留位置01" forState:UIControlStateNormal];
        btn.frame = CGRectMake(btn2X + margin, baseY + btnWidthAndHeight * 3 + margin*3, btnWidthAndHeight, btnWidthAndHeight);
        btn.backgroundColor = [UIColor whiteColor];
        _LeaveFunctionBtn_No1 = btn;
        return btn;
    }
    return _LeaveFunctionBtn_No1;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
